// The payment page
// ================

define(['zepto', 'utils', 'wxupload', 'cityPicker'], function($, utils, wxupload) {
  return {

    init: function(data) {
      
      this.bindSubmitAction();
      
      this.collapseMenu()
      
    },


    // order submit evvent handler
    bindSubmitAction: function() {
      
      var thatSubmit = this
      
      var submitButton = $("a.button-submit")
      
      submitButton.on('click', function(e){
        e.preventDefault()
        // init all data
        var data = thatSubmit.onInitData()
        // validate all input
        if(thatSubmit.onValidateData(data)) {
          // do submit order with data
          data = thatSubmit.onAppendData(data)
          thatSubmit.onSubmitOrder(data)
        }
      })
      
    },
    
    collapseMenu: function() {
        $(document).on("change", "input[name='source']", function(){
            if($(this).prop("checked")) {
                var values=$(this).val();
                if(values =='F'){
                    $("#source").addClass("active");
                }
                else{
                    $("#source").removeClass("active").find("textarea").val("");
                }
            }
        });
        $(document).on("change", "input[name='reason']", function(){
            if($(this).prop("checked")) {
                var values=$(this).val();
                if(values =='E'){
                    $("#reason").addClass("active");
                }
                else{
                    $("#reason").removeClass("active").find("textarea").val("");
                   
                }
            }
        });
        $(document).on("change", "input[name='expectation']", function(){
            if($(this).prop("checked")) {
                var values=$(this).val();
                if(values =='D'){
                    $("#expectation").addClass("active");
                }
                else{
                    $("#expectation").removeClass("active").find("textarea").val("");
                }
            }
        })
    },

    // init data
    onInitData: function() {
      return {
        '_method': {
          'value': $('input[name="_method"]').val().trim(),
          'message': '提交方式不能为空'
        },
        'source': {
            'value': $('input[name="source"]:checked').val(),
            'message': '请勾选得知线上减脂营渠道'
         },
         'reason': {
             'value': $('input[name="reason"]:checked').val(),
             'message': '请勾选参加训练营的主要因素'  
         },
         'expectation': {
             'value': $('input[name="expectation"]:checked').val(),
             'message': '请勾选您想达到的效果'  
         }
        
         
      }
    },

    onValidateData: function(data) {
      var that = this
      var valid = _.find(data, function(item) {
        return !item.value
      })

      if (!!valid) return $.toast(valid.message)
      
      if(!that.validateCheckbox()) {
          return false;
      }
      
      return true
    },

    validateCheckbox: function() {
        var result = true;
        $(".survey-descr").each(function() {
            if($(this).hasClass("active")){
                var value = $(this).find("textarea").val();
                var message =$(this).find("textarea").data("message");
                if(value == null || value == '') {
                    result = false;
                    return $.toast(message)
                }
             } 
        })
        
        
                
        return result;
    },
    
    onAppendData: function(data) {
      
      var orderId = $("input[name='orderId']").val();
      if(orderId) {
        data.orderId = {
          'value': orderId
        }
      }
      
      var sourceRemarks = $("textarea[name='sourceRemarks']").val();
      if(sourceRemarks) {
        data.sourceRemarks = {
          'value': sourceRemarks
        }
      }
      
      var reasonRemarks = $("textarea[name='reasonRemarks']").val();
      if(reasonRemarks) {
        data.reasonRemarks = {
          'value': reasonRemarks
        }
      }
      expectionRemarks
      var expectionRemarks = $("textarea[name='expectionRemarks']").val();
      if(expectionRemarks) {
        data.expectionRemarks = {
          'value': expectionRemarks
        }
      }
      
      
      var camp = $("input[name='camp']").val();
      if(camp) {
        data.camp = {
          'value': camp
        }
      }
      
      var coach = $("input[name='coach']").val();
      if(coach) {
        data.coach = {
          'value': coach
        }
      }
      
      return data;
    },

    onSubmitOrder: function(data) {
      
      $.showPreloader('正在提交')

      var processedData = _.mapObject(data, function(obj) {
        return obj.value
      })
      $.ajax({
        type: 'POST',
        url: __CTX__.root + '/investigations/participate',
        data: processedData
      }).done(function(response) {
        $.hidePreloader()
        utils.processResult(response, function(result) {
          $.toast('填写其他调查成功')
          setTimeout(function() {
            window.location.href = __CTX__.root + '/investigations'
          }, 1000)
        }, function(result) {
          $.toast(result.message || '填写其他调查失败')
        })
      })
      
    }
  }
})